﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common.Localization;
using Common.Protocol;

namespace UI
{
    public partial class CellContainer : UserControl, ILocalizable
    {
        private int rowsCount, colsCount;

        private List<Cell> Cells;

        public CellContainer()
        {
            this.rowsCount = 0;
            this.colsCount = 0;
            this.Cells = new List<Cell>();
            this.InitializeComponent();
        }

        public event EventHandler ShowExtendedCell;

        public event EventHandler<Cell> DeactivateChannel;

        public void AddCell(Cell cell)
        {
            if (this.Cells.Count == 0)
            {
                // додаєм першу комірку
                cell.Top = Cell.Margin;
                cell.Left = Cell.Margin;
                this.colsCount++;
                this.rowsCount++;
            }
            else
            {
                cell.Left = this.Cells.Last().Right + 2 * Cell.Margin;
                cell.Top = this.Cells.Last().Top;
                if(this.rowsCount == 1)
                {
                    this.colsCount++;
                }

                // Комірка висувається за межі контейнера - переносим в наступний рядок.
                if (cell.Right > this.Width)
                {
                    cell.Top = this.Cells.Last().Bottom + 2 * Cell.Margin;
                    cell.Left = Cell.Margin;
                    this.rowsCount++;
                }
            }

            CellMover.CellContainer = this;
            CellMover.Init(cell);
            this.Cells.Add(cell);
            this.Controls.Add(cell);
            cell.CellDecoratorClicked += this.CellCloseEventHandler;
            cell.CellOptionsClicked += this.CellOptionClickedEventHandler;
        }

        public void RemoveCell(Cell cell)
        {
            this.Cells.Remove(cell);
            this.Controls.Remove(cell);
            this.AllocateCells();

            if (this.DeactivateChannel != null)
            {
                this.DeactivateChannel(this, cell);
            }
        }

        public void Clear()
        {
            foreach(Cell cell in this.Cells)
            {
                this.Controls.Remove(cell);
            }

            this.Cells.Clear();
        }

        public void Localize()
        {
            foreach (var control in this.Controls)
            {
                try
                {
                    (control as ILocalizable).Localize();
                }
                catch (Exception)
                {
                }
            }
        }

        private void AllocateCells()
        {
            for (int i = 0; i < this.Cells.Count; i++)
            {
                Cell currentCell = this.Cells[i];
                int index = this.Controls.IndexOf(currentCell);
                if (index != -1)
                {
                    if (i == 0)
                    {
                        this.Controls[index].Left = Cell.Margin;
                        this.Controls[index].Top = Cell.Margin;
                    }
                    else
                    {
                        int expectedRight = this.Cells[i - 1].Right + 2 * Cell.Margin + Cell.Width;
                        if(expectedRight > this.Width)
                        {
                            this.Controls[index].Left = Cell.Margin;
                            this.Controls[index].Top = this.Cells[i - 1].Bottom + 2 * Cell.Margin;
                        }
                        else
                        {
                            this.Controls[index].Left = this.Cells[i - 1].Right + 2 * Cell.Margin;
                            this.Controls[index].Top = this.Cells[i - 1].Top;    
                        }
                    }
                }
            }
        }

        public void GetCellPositionInfo(Cell cell, out bool over, out bool under, out bool left, out bool right)
        {
            int index = this.Cells.IndexOf(cell);
            if (index != -1)
            {
                int row = index / this.colsCount;
                int col = index / this.rowsCount;
                over = row > 1;
                left = col > 1;
                if (col == colsCount)
                {
                    right = false;
                }
                else
                {
                    right = index + 1 > this.Cells.Count;    
                }

                if (row == rowsCount)
                {
                    under = false;
                }
                else
                {
                    under = index + colsCount >= this.Cells.Count;
                }
            }
            else
            {
                throw new ArgumentException("CellContainer does not contain specified cell");
            }
        }

        #region cells container event handlers

        private void CellContainerResizeEventHandler(object sender, EventArgs e)
        {
            int newColsCount = this.Width / (Cell.Width + 2 * Cell.Margin);
            if (this.HorizontalScroll.Visible)
            {
                int w = this.Width;
            }

            if (newColsCount != this.colsCount)
            {
                if (this.Cells.Count > this.colsCount)
                {
                    try
                    {
                        List<Cell> c = this.Cells.ToList();
                        foreach (var cell in this.Cells)
                        {
                            this.Controls.Remove(cell);
                        }

                        this.Cells.Clear();
                        foreach (var cell in c)
                        {
                            this.AddCell(cell);
                        }

                        this.colsCount = newColsCount;
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }

        private void CellContainerLoad(object sender, EventArgs e)
        {
            this.colsCount = this.Width / (Cell.Width + 2 * Cell.Margin);
        }

        #endregion

        #region cells event handlers

        private void CellCloseEventHandler(object sender, EventArgs e)
        {
            this.RemoveCell(sender as Cell);
        }

        private void CellOptionClickedEventHandler(object sender, EventArgs e)
        {
            if(this.ShowExtendedCell != null)
            {
                this.ShowExtendedCell(sender, null);
            }
        }

        #endregion
    }
}